/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2016-4-12 13:56:00
 ******************************************************************************/
package cn.com.xy.sms.util;

import java.util.HashMap;
import java.util.Map;


import org.json.JSONObject;

import android.content.ContentValues;
import android.os.Process;
import cn.com.xy.sms.sdk.Iservice.XyCallBack;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.base.BaseManager;
import cn.com.xy.sms.sdk.db.entity.MatchCacheManager;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.service.baseparse.BaseParseService;
import cn.com.xy.sms.sdk.threadpool.SmartSmsThreadPoolManager;
import cn.com.xy.sms.sdk.util.JsonUtil;
import cn.com.xy.sms.sdk.util.SdkCache;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.sdk.util.XyUtil;
/*SDK-616 hewengao 20160912 end*/
/*SDK-635 songzhirong 20161011 start*/
import org.json.JSONArray;
import org.json.JSONException;
/*SDK-635 songzhirong 20161011 end*/
/**
 * 丰富气泡、简单气泡、特征值识别解析接口，解析短信时传入需要解析类型的flag
*/
public class ParseSmsMessage {
    //parse flag
    public final static int DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG = 1;
    public final static int DUOQU_SMARTSMS_SHOW_BUBBLE_RICH_FLAG = 2;
    public final static int DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG = 8;
    public final static int DUOQU_SMARTSMS_SHOW_URL_VALUE_FLAG = 16;
    /* RM-1374 mobaohua 20170406 start */
    public final static int DUOQU_SMARTSMS_SHOW_URL_PREVIEW_VALUE_FLAG = 24;
    /* RM-1374 mobaohua 20170406 end */
    /* RM-1467 mobaohua 20170406 start */
    public final static int DUOQU_SMARTSMS_SHOW_URL_MSG_MOVIE_FLAG = 32;
    /* RM-1467 mobaohua 20170406 end */
    /* SDK-765 mobaohua 20170419 start */
    public final static int DUOQU_SMARTSMS_SHOW_URL_EXPRESS_INFO_FLAG = 36;
    /* SDK-765 mobaohua 20170419 end */
    
    //has data
    public final static int DUOQU_CALLBACK_UITHREAD_HASDATA = 0;
    public final static int DUOQU_CALLBACK_BACKTHREAD_HASDATA = 1;
    
    /*SDK-514 zhaojiangwei 20160630 start*/
    public final static int DUOQU_CALLBACK_REFRESH_LIST = 2;
    /*SDK-514 zhaojiangwei 20160630 end*/
    
    //parsing status
    public final static int DUOQU_CALLBACK_UITHREAD_NEEDPARSE = -2;
    //local error status
    public final static int DUOQU_CALLBACK_UITHREAD_NODATA = -1;
    public final static int DUOQU_CALLBACK_UITHREAD_SCOLLING = -4;
    public final static int DUOQU_CALLBACK_BACKTHREAD_FAIL = -3;
    //global error status
    public final static int DUOQU_CALLBACK_UITHREAD_PHONENUM_NULL = -1;
    public final static int DUOQU_CALLBACK_UITHREAD_UNKOWN = -5;

    public static void parseMessage(final String msgId, final String phoneNum, final String smsCenterNum,
            final String smsContent, final long smsReceiveTime, int parseFlag, HashMap<String, Object> extend,
            final SdkCallBack xyCallBack, boolean scrollFing) {
        try {
            if(parseFlag == DUOQU_SMARTSMS_SHOW_BUBBLE_RICH_FLAG) {
                //rich bubble
                ParseRichBubbleManager.queryDataByMsgItem(msgId, phoneNum, smsContent,
                        smsReceiveTime, smsCenterNum, 2, xyCallBack, scrollFing, extend);
            } else if(parseFlag == DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG) {
                //simple bubble
            	/*SDK-604 zhaojiangwei 20160901 start */
            	Map<String, String>extMap = new HashMap<String, String>();
            	if (extend!=null && extend.containsKey("isUseNewAction")) {
            		extMap.put("isUseNewAction", extend.get("isUseNewAction").toString());
				}
            	/*SDK-604 zhaojiangwei 20160901 end */
                ParseBubbleManager.queryDataByMsgItem(msgId, phoneNum, smsContent,
                        smsCenterNum, 1, smsReceiveTime, xyCallBack, scrollFing, extMap);
            } else if(parseFlag == DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG) {
                /*SDK-635 songzhirong 20161011 start*/
                if(XyUtil.isFlagOn("isCombineAction", extend)){
                    parseRecognisedValueCombineAction(msgId, phoneNum,
                            smsCenterNum, smsContent, smsReceiveTime, extend, xyCallBack, scrollFing);
                }else{
                    //recognize value
                    parseRecognisedValue(msgId, phoneNum, smsCenterNum, smsContent,
                            smsReceiveTime, extend, xyCallBack, scrollFing); 
                }
                /*SDK-635 songzhirong 20161011 end*/
              
            }
        } catch (Throwable e) {
            LogManager.e("XIAOYUAN", "parseMessage error type:" , e);
            /* SDK-584 zhaojiangwei 20160906 start */
            DexUtil.saveExceptionLog(e);
            /* SDK-584 zhaojiangwei 20160906 end */
        }
    }

    public static void parseRecognisedValue(final String msgId,final String phoneNum, final String smsCenterNum,
            final String smsContent, final long smsReceiveTime,final HashMap<String,Object> extend,
            final SdkCallBack xyCallBack, boolean scrollFing) throws Exception {
        /*SDK-484 zhaojiangwei 20160614 start*/
        LogManager.e("rec_parse", "parseRecognisedValue,msgId:"+msgId+",sms:"+smsContent+",num:"+phoneNum);
    	if (smsContent == null) {
            throw new Exception("msgid or phoneNum or smsContent is null but they need value.");
        }
    	/*SDK-484 zhaojiangwei 20160614 end*/
    	
        JSONObject res = null;
        final SdkCache sdkCache = SdkCache.createSdkCache(phoneNum);
        res = sdkCache.effectiveRecognisedValue.get(msgId);
        if (res != null) {
            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_UITHREAD_HASDATA,
                    res, msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
            return;
        }
        if (sdkCache.invalidRecognisedValue.contains(msgId)) {
            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_UITHREAD_NODATA,
                    " invalidRecognisedValue", msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
            return;
        }

        if (sdkCache.inQueueRecognisedValue.contains(msgId)) {
            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_UITHREAD_NODATA,
                    " inQueueRecognisedValue", msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
            return;
        }
        /*SDK-492 hewengao 20160706 start*/
        JSONObject cacheObj = sdkCache.cacheRecognisedValue.remove(msgId);
        JSONObject dataArr = (JSONObject) JsonUtil.getValueFromJsonObject(cacheObj, "value_recognise_result");
        if(cacheObj != null && dataArr != null){
            String md5 = (String) JsonUtil.getValueFromJsonObject(cacheObj,"msg_num_md5");
            String md5_1 = MatchCacheManager.getMD5(phoneNum, smsContent);
            if (md5_1 != null && md5 != null && md5_1.equals(md5)) {
               synchronized (sdkCache.effectiveRecognisedValue) {
                   sdkCache.effectiveRecognisedValue.put(msgId, dataArr);
                   sdkCache.invalidRecognisedValue.remove(msgId);
               }
               XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_UITHREAD_HASDATA,
                     dataArr, msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
               return;
            }
        }
        /*SDK-492 hewengao 20160706 end*/
        if (!scrollFing) {
            /* huangzhiqiang 20160927 start */
            sdkCache.inQueueRecognisedValue.add(msgId);
            /* huangzhiqiang 20160927 end */
            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_UITHREAD_NEEDPARSE,
                    " need parse ", msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
            /*SDK-616 hewengao 20160918 start*/
            SmartSmsThreadPoolManager.richPool.execute(new Runnable() {
            /*SDK-616 hewengao 20160918 end*/
                public void run() {
                    try {
                        /*SDK-616 hewengao 20160918 start*/
                        SmartSmsThreadPoolManager.setThreadNameAndPriority(SmartSmsThreadPoolManager.TNAME_RICHPOOL, 
                                Process.THREAD_PRIORITY_BACKGROUND);
                        /*SDK-616 hewengao 20160918 end*/
                        Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);
                        
                        /*SDK-716 zhaojiangwei 20170107 start*/
                        /*SDK-518 zhaojiangwei 20160705 start*/
                        if (!ParseManager.isInitData()) {
                            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_BACKTHREAD_FAIL,
                                    "sdk is still in init", msgId,
                                    ParseSmsMessage.DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
                            return;
                        }
                        /*SDK-518 zhaojiangwei 20160705 end*/
                        /*SDK-716 zhaojiangwei 20170107 end*/
                        
                        JSONObject dataArr = null;
                        JSONObject res = queryRecognisedValueFromDb(msgId, phoneNum, smsContent, smsReceiveTime);
                        if(res != null && !res.has("need_parse_recognise")) {
                            dataArr = (JSONObject) JsonUtil.getValueFromJsonObject(res, "value_recognise_result");
                            if(dataArr != null && !res.has("need_parse_recognise")) {
                                synchronized (sdkCache.effectiveRecognisedValue) {
                                    sdkCache.effectiveRecognisedValue.put(msgId, dataArr);
                                }
                                sdkCache.invalidRecognisedValue.remove(msgId);
                                XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_BACKTHREAD_HASDATA,
                                      dataArr, msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
                            } else {
                                sdkCache.invalidRecognisedValue.add(msgId);
                                    XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_BACKTHREAD_FAIL,
                                            " invalid data need_parse_simple", msgId,
                                            ParseSmsMessage.DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
                            }
                            return;
                        }
                        if(dataArr == null){
                        	/*SDK-409 hewengao 20160426 start*/
                            dataArr = queryRecognisedValueFromApi(msgId,
                                    phoneNum, smsCenterNum, smsContent,smsReceiveTime,extend, xyCallBack);
                            /*SDK-409 hewengao 20160426 start*/
                        }
                        if (dataArr != null) {
                            synchronized (sdkCache.effectiveRecognisedValue) {
                                sdkCache.effectiveRecognisedValue.put(msgId, dataArr);
                            }
                            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_BACKTHREAD_HASDATA,
                                  dataArr, msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
                            LogManager.e("rec_parse", "vals from db:msgId"+msgId+",sms:"+smsContent+",rets:"+dataArr.toString());
                        } else {
                            sdkCache.invalidRecognisedValue.add(msgId);
                            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_BACKTHREAD_FAIL,
                                    dataArr, msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
                        }
                    } catch (Throwable e) {
                        LogManager.e("duoquTest",e.getLocalizedMessage(),e );
                        XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_BACKTHREAD_FAIL,
                                "result is null: error: " + e.getMessage(), msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
                    } finally {
                        sdkCache.inQueueRecognisedValue.remove(msgId);
                    }
                }
            });
        } else {
            XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_UITHREAD_SCOLLING,
                    " is scrolling", msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
        }
    }

    private static JSONObject queryRecognisedValueFromDb(String msgId,String phoneNum,
            String smsContent,long smsReceiveTime) {
        JSONObject res = null;
        //from db query
        res = MatchCacheManager.getDataByParam(msgId);
        if(res != null){
            String md5 = (String) JsonUtil.getValueFromJsonObject(res,"msg_num_md5");
            String md5_1 = MatchCacheManager.getMD5(phoneNum,smsContent);
            if(null != md5 && md5.equals(md5_1)){
                return res;
            }
        }
        return null;
    }

    //特征值解析入口，在基础解析也会调用
    public static JSONObject queryRecognisedValueFromApi(final String msgId, final String phoneNum,
			final String centerNum, final String smsContent, final long smsReceiveTime, final Map extend,final SdkCallBack xyCallBack) {
    	 /*SDK-458 hewengao 20160525 start */
    	long startTime = System.currentTimeMillis();
    	boolean refBaseValue = false;
    	Map<String, Object> tmpMap = new HashMap<String, Object>();
		try {
	    	if(extend != null){
	    		/*SDK-490 hewengao 20160616 start*/
	    		Object obj =extend.get("ref_basevalue");
	    		extend.remove("parse_recognise_value");
	    		if(obj != null){
	    			refBaseValue =Boolean.valueOf(obj.toString());
	    		}
	    		/*SDK-490 hewengao 20160616 end*/
	    		tmpMap.putAll(extend);
	    	}
	    	/* SDK-514 zhaojiangwei 20160630 start */
	    	if(refBaseValue){
	    	    /*SAMSUNGCS-534 zhaojiangwei 20160930 start*/
	    	    String key = msgId+smsContent+phoneNum;
	    	    final String keyHash = String.valueOf(key.hashCode());
	    		final SdkCache sdkCache = SdkCache.createSdkCache(phoneNum); 
				if (sdkCache.baseRefsValue.containsKey(keyHash)) {
					tmpMap.putAll(sdkCache.baseRefsValue.remove(keyHash));
				}
				/*SAMSUNGCS-534 zhaojiangwei 20160930 end*/
				else{
					//异步发起基础识别
					XyCallBack parseCallBack = new XyCallBack() {
					    @Override
						public void execute(Object... obj) {
							if (obj != null) {
								try {
								  
									Map<String, Object> map = (Map<String, Object>) obj[0];
									sdkCache.baseRefsValue.put(keyHash, map);

									sdkCache.effectiveRecognisedValue.remove(msgId);
									
									/* SDK-528 zhaojiangwei 20160711 start*/
									sdkCache.inQueueRecognisedValue.remove(msgId);
									/* SDK-528 zhaojiangwei 20160711 end*/
									
									deleteRecognisedResultFromDb(msgId);

									//走刷新机制，重新识别.
									XyUtil.doXycallBackResult(xyCallBack, DUOQU_CALLBACK_REFRESH_LIST, "refresh list",
											msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
								}
								catch (Throwable e) {
									LogManager.e(
											Constant.TAG,
											"ParseSmsMessage queryRecognisedValueFromApi error:"
											 , e);
								}
							}
						}
					};
					BaseParseService.parseMsgAsyn(Constant.getContext(), msgId, phoneNum, centerNum, smsContent, smsReceiveTime, extend, parseCallBack);
				}
	    	}
	    	/* SDK-514 zhaojiangwei 20160630 end */
		} 
		catch (Exception e) {
            /*SDK-536 hewengao/20160713 start*/
		    LogManager.e("rec_parse", "Expection in queryRecognisedFromApi:"+e);
            /*SDK-536 hewengao/20160713 end*/
            /* SDK-584 zhaojiangwei 20160906 start */
            DexUtil.saveExceptionLog(e);
            /* SDK-584 zhaojiangwei 20160906 end */
		}
		 /*SDK-458 hewengao 20160525 end */
    	/*SDK-409 hewengao 20160426 start*/
        JSONObject dataStr = ParseManager.parseRecogniseValue(phoneNum,smsContent,smsReceiveTime, tmpMap);
        /*SDK-409 hewengao 20160426 start*/
        if(dataStr != null) {
            LogManager.e("rec_parse", "vals from api:msgId"+msgId+",sms:"+smsContent+",rets:"+dataStr.toString());
            saveRecognisedResultToDb(msgId, phoneNum, centerNum, smsContent, smsReceiveTime, dataStr.toString());
        }else{
            LogManager.e("rec_parse", "vals from api:msgId"+msgId+",sms:"+smsContent+",rets:null");
            saveRecognisedResultToDb(msgId, phoneNum, centerNum, smsContent, smsReceiveTime, "");
        }
        return dataStr;
    }

    private static void saveRecognisedResultToDb(final String msgId,
            final String phoneNumber, final String smsCenterNum,
            final String smsContent,final long smsReceiveTime,final String data) {

        try {
            String msg_num_md5 = MatchCacheManager.getMD5(phoneNumber,
                    smsContent);
            if (!StringUtils.isNull(msg_num_md5)) {
                ContentValues matchCache = BaseManager.getContentValues(null,
                        "msg_num_md5", msg_num_md5, "phonenum", StringUtils.getPhoneNumberNo86(phoneNumber),
                        "msg_id",msgId, "value_recognise_result", data == null ? "": data,
                        "recognise_lasttime", String.valueOf(System.currentTimeMillis()));
                MatchCacheManager.insertOrUpdate(matchCache, 4);
            }
        } catch (Throwable e) {
            LogManager.e("rec_parse", e.getMessage(), e);
        }
    }
    
    /*SDK-514 zhaojiangwei 20160630 start */
	private static void deleteRecognisedResultFromDb(String msgId){
		LogManager.d("duoquTest", "deleteRecognised Result FromDb:",msgId);
		MatchCacheManager.resetRecognisedResult(msgId);
	}
	/*SDK-514 zhaojiangwei 20160630 end */
	
	 /*SDK-635 songzhirong 20161011 start*/
	 public static void parseRecognisedValueCombineAction(final String msgId,final String phoneNum, final String smsCenterNum,
	            final String smsContent, final long smsReceiveTime,final HashMap<String,Object> extend,
	            final SdkCallBack xyCallBack, final boolean scrollFing) throws Exception {
	   
	     LogManager.e("rec_parse", "parseRecognisedValueCombineAction:msgId"+msgId+",sms:"+smsContent+",num:"+phoneNum);
	     final SdkCallBack recognisedValueCallBack = new SdkCallBack(){
	            @Override
	            public void execute(final Object... recognisedValueObj) {
	               
	                try {
	                    Map<String, String>extMap = new HashMap<String, String>();
	                    if (extend!=null && extend.containsKey("isUseNewAction")) {
	                        extMap.put("isUseNewAction", extend.get("isUseNewAction").toString());
	                    }
	                    ParseBubbleManager.queryDataByMsgItem(msgId, phoneNum, smsContent,
	                            smsCenterNum, 1, smsReceiveTime,  new SdkCallBack(){

	                        @Override
	                        public void execute(Object... bubbleValueObj) {
	                            try {

	                                JSONArray actionData = null;
	                                final int bubbleValueStatus = (Integer)bubbleValueObj[0];
	                                switch(bubbleValueStatus){
	                                case ParseSmsMessage.DUOQU_CALLBACK_UITHREAD_HASDATA:
	                                case ParseSmsMessage.DUOQU_CALLBACK_BACKTHREAD_HASDATA:
	                                    if(bubbleValueObj[1] instanceof JSONArray) {
	                                        actionData = (JSONArray)bubbleValueObj[1];
	                                    }
	                                   break;
	                                }   
	                                
	                                final int recognisedStatus = (Integer)recognisedValueObj[0];
	                                switch (recognisedStatus) {
	                                case ParseSmsMessage.DUOQU_CALLBACK_UITHREAD_HASDATA:
	                                case ParseSmsMessage.DUOQU_CALLBACK_BACKTHREAD_HASDATA:
	                                    JSONObject jsonData = (JSONObject) recognisedValueObj[1]; 
	                                    if(actionData!=null){
	                                        try {
	                                            jsonData.put("NEW_ADACTION", actionData);
	                                        } catch (JSONException e) {
	                                            // TODO Auto-generated catch block
	                                            LogManager.e(Constant.TAG, e.getMessage(), e);
	                                        }
	                                    }
	                                    if(bubbleValueStatus == ParseSmsMessage.DUOQU_CALLBACK_UITHREAD_HASDATA 
	                                            && recognisedStatus == ParseSmsMessage.DUOQU_CALLBACK_UITHREAD_HASDATA){
	                                        XyUtil.doXycallBackResult(xyCallBack, recognisedValueObj);
	                                    }else{
	                                           XyUtil.doXycallBackResult(xyCallBack, ParseSmsMessage.DUOQU_CALLBACK_BACKTHREAD_HASDATA,
	                                                   jsonData,recognisedValueObj[2],recognisedValueObj[3]);
	                                    }
	                                    break;                            
	                                default:     
	                                    if(actionData!=null){
	                                        JSONObject temp = new JSONObject();
	                                        try {
	                                            temp.put("NEW_ADACTION", actionData);
	                                        } catch (JSONException e) {
	                                            // TODO Auto-generated catch block
	                                            LogManager.e(Constant.TAG, e.getMessage(), e);
	                                        }
	                                        XyUtil.doXycallBackResult(xyCallBack, bubbleValueStatus,
	                                                temp, msgId, DUOQU_SMARTSMS_SHOW_RECOGNISE_VALUE_FLAG);
	                                    }else{
	                                        XyUtil.doXycallBackResult(xyCallBack, recognisedValueObj);
	                                    }
	                                    break;
	                                }
	                            
                                } catch (Throwable e) {
                                    LogManager.e(Constant.TAG, "parseRecognisedValueCombineAction ", e);

                                }
	                        }
	                         
	                    }, scrollFing, extMap);
                    } catch (Throwable e) {
                        LogManager.e(Constant.TAG, "parseRecognisedValueCombineAction1 ", e);
                    }
	            
	            }
	             
	     };
	     parseRecognisedValue(msgId, phoneNum, smsCenterNum, smsContent,
                 smsReceiveTime, extend, recognisedValueCallBack, scrollFing); 
	 }
	 /*SDK-635 songzhirong 20161011 end*/

    /* SDK-743 zhaojiangwei 20170329 start */
    /**
     * 判断是否推广类退订短信，不能识别卡片且短信内容包含“退订”、“退订单”、“退订货”关键字的则认为是推广类退订短信
     * 
     * @param msgId
     * @param phoneNum
     * @param smsCenterNum
     * @param smsContent
     * @param smsReceiveTime
     * @param extend
     * @return
     */
    public static boolean parseSpecailSms(String msgId, String phoneNum, String smsCenterNum, String smsContent,
            long smsReceiveTime, HashMap<String, Object> extend) {
        if (DexUtil.isUnSubMsg(smsContent)) {
            try {
                Map<String, Object> resultMap = BaseParseService.parseMsg(Constant.getContext(), phoneNum,
                        smsCenterNum, smsContent, smsReceiveTime, XyUtil.changeObjMapToStrMap(extend));
                return resultMap == null || ParseBubbleManager.getParseStatu(resultMap) == -1;
            } catch (Throwable t) {
                LogManager.e(Constant.TAG, "ParseSmsMessage parseSpecailSms error", t);
            }
        }
        return false;
    }
    /* SDK-743 zhaojiangwei 20170329 end */

    /* SMARTISAN-565 huangzhiqiang 20170413 start */
    /**
     * 识别短信是否是工资类短信，如果是则返回JSONArray数据，里面包含有工资类短信的金额下标；
     * 
     * @param msg 短信原文
     * @return 
     *         非工资短信返回：null
     *         是工资短信返回：[ {"startIndex":40, "endIndex":47 },{"startIndex":53, "endIndex":60 }]
     */
    public static JSONArray parseSalaryIndex(String msg) {
        try {
            String result = DexUtil.parseSalary(msg);
            if (StringUtils.isNull(result)) {
                return null;
            }
            JSONObject resultJson = new JSONObject(result);
            if (!resultJson.optBoolean("salaryScene") || !resultJson.has("indexs")) {
                return null;
            }
            return resultJson.optJSONArray("indexs");
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "ParseSmsMessage parseSalary error", e);
        }
        return null;
    }
    /* SMARTISAN-565 huangzhiqiang 20170413 end */

    /* SDK-766 huangzhiqiang 20170419 start */
    /**
     * 识别短信内容是否存在退订信息，如果存在则返回JSONArray数据，数据包含退订指令或退订号码(instruct)、动作类型(actionType 1:短信回复类的退订、2:电话类的退订)、退订内容下标(startIndex,endIndex)
     * 
     * @param msg
     * @return
     *         不存在退订信息返回：null
     *         存在退订信息返回：[{"instruct":"95582","actionType":2,"startIndex":0,"endIndex":9},{"instruct":"N","actionType":1,"startIndex":74,"endIndex":79}]
     */
    public static JSONArray parseUnsubscribe(String msg) {
        try {
            String result = DexUtil.parseUnsubscribe(msg);
            if (StringUtils.isNull(result)) {
                return null;
            }
            JSONArray resultJsonArr = new JSONArray(result);
            return resultJsonArr.length() == 0 ? null : resultJsonArr;
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "ParseSmsMessage parseUnsubscribe error", e);
        }
        return null;
    }
    /* SDK-766 huangzhiqiang 20170419 end */
}
